package edu.westga.recommender.model;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
import org.apache.mahout.cf.taste.recommender.ItemBasedRecommender;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.xml.sax.InputSource;

public class ItemItemSimilarity {
	
	public ItemItemSimilarity(){
	}
	
	public String RecommendNow(long userId, File recsFile){
	try{
	    String docIdsTitle = "docIdsTitles.xml";
	    //Integer neighborhoodSize = Integer.parseInt(jTextFieldNSize.getText());
	    InputSource is = new InputSource(new FileInputStream(docIdsTitle));
	    SAXParserFactory factory = SAXParserFactory.newInstance();
	    factory.setValidating(false);
	    SAXParser sp = factory.newSAXParser();
	    ContentHandler handler = new ContentHandler();
	    sp.parse(is, handler);
	    
	    //create the data model
	    FileDataModel dataModel = new FileDataModel(recsFile);
	    System.out.println("Data Model: Users: " + dataModel.getNumUsers() + " Items: " + dataModel.getNumItems());
	    
	    //Create an ItemSimilarity
	    ItemSimilarity itemSimilarity = new LogLikelihoodSimilarity(dataModel);
	    //Create an Item Based Recommender
	    ItemBasedRecommender recommender =
	            new GenericItemBasedRecommender(dataModel, itemSimilarity);
	    
	    System.out.println("-----");
	    System.out.println("User: " + userId);
	    
	    //Get the top 5 recommendations
	    List<RecommendedItem> simItems =
	            recommender.recommend(userId, 5);
	    return TasteUtils.printRecs(simItems, handler.map);
	  }catch (Exception e){
		return e.getMessage();
	  }
	}
}
